package com.example.app.ui.health;
import static android.content.Context.MODE_PRIVATE;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import com.example.app.R;
import com.example.app.databinding.FragmentHealthBinding;
import com.example.app.ui.health.Doctor.HealthNewsActivity;
import com.example.app.ui.health.Doctor.PatientInfoActivity;
import com.example.app.ui.health.Doctor.PatientReportActivity;
import com.example.app.ui.health.User.AdviceActivity;
import com.example.app.ui.health.User.BleTooth.BleTooth;
import com.example.app.ui.health.User.HistoryActivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class HealthFragment extends Fragment {
    private ListView healthListView;
    private FragmentHealthBinding binding;
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentHealthBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        // 初始化ActionBar
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        ActionBar supportActionBar = activity.getSupportActionBar();
        if (supportActionBar != null) {
            View customView = supportActionBar.getCustomView();
            TextView textViewTitle = customView.findViewById(R.id.textView_title);
            textViewTitle.setText("健康服务");
        }
        healthListView = root.findViewById(R.id.healthlv);
        List<Map<String, Object>> menuItems = new ArrayList<>();
        SharedPreferences prefs = this.getActivity().getSharedPreferences("user_credentials", MODE_PRIVATE);
        String Role = prefs.getString("Role", "");
        System.out.println(Role);
        if (Role.equals("user")){
            addMenuItem(menuItems, R.drawable.heart_rate, "测心率");
            addMenuItem(menuItems, R.drawable.heart_rate_history, "历史记录");
            addMenuItem(menuItems, R.drawable.advice, "医生指导意见");
            addMenuItem(menuItems, R.drawable.advice, "最近新闻");
        }
        else if (Role.equals("doctor")) {
            addMenuItem(menuItems, R.drawable.heart_rate_report, "查看病人心率报告");
            addMenuItem(menuItems, R.drawable.patientinfo, "病人信息");
            addMenuItem(menuItems, R.drawable.advice, "最近新闻");
        }
        HealthMenuAdapter adapter = new HealthMenuAdapter(requireContext(), menuItems);
        healthListView.setAdapter(adapter);
        healthListView.setOnItemClickListener((parent, view, position, id) -> {
            if (Role.equals("user")) {
                handleUserMenuItemClick(position);
            } else if (Role.equals("doctor")) {
                handleDoctorMenuItemClick(position);
            }
        });
        return root;
    }
    private void handleUserMenuItemClick(int position) {
        Intent intent;
        switch (position) {
            case 0:
                intent = new Intent(getContext(), BleTooth.class);
                break;
            case 1:
                intent = new Intent(getActivity(), HistoryActivity.class);
                break;
            case 2:
                intent = new Intent(getActivity(), AdviceActivity.class);
                break;
            default:
                return;
        }
        startActivity(intent);
    }

    private void handleDoctorMenuItemClick(int position) {
        Intent intent;
        switch (position) {
            case 0:
                intent = new Intent(getActivity(), PatientReportActivity.class);
                break;
            case 1:
                intent = new Intent(getActivity(), PatientInfoActivity.class);
                break;
            case 2:
                intent = new Intent(getActivity(), HealthNewsActivity.class);
                break;
            default:
                return;
        }
        startActivity(intent);
    }
    private void addMenuItem(List<Map<String, Object>> list, int iconRes, String title) {
        Map<String, Object> item = new HashMap<>();
        item.put("icon", iconRes);
        item.put("title", title);
        list.add(item);
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}